#include "frmprincipalimpl.h"
//
frmPrincipalImpl::frmPrincipalImpl( QWidget * parent, Qt::WFlags f) 
	: QMainWindow(parent, f)
{
	setupUi(this);
	
	//Cargo Label de prueba
	QLabel *desc=new QLabel("Descripcion del documento con link superior");
	QLabelExtClick *qlec=new QLabelExtClick("",NULL);
	
    QPalette palette;
    QBrush brush(QColor(85, 0, 255, 255));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::WindowText, brush);
    QBrush brush1(QColor(0, 0, 255, 255));
    brush1.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Text, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::WindowText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Text, brush1);
    QBrush brush2(QColor(146, 145, 144, 255));
    brush2.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush2);
    QBrush brush3(QColor(165, 164, 164, 255));
    brush3.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Disabled, QPalette::Text, brush3);

	qlec->setPalette(palette);
	qlec->setText("Documento1");
	qlec->setCursor(QCursor(Qt::PointingHandCursor));
	
	qlec->setPdfPath("PathPrueba");
	string strr("asfd");
	connect(qlec, SIGNAL(clicked(char*)), this, SLOT(onlinkPdfclicked(char*)));
	
	//Creo contenedor de Labels
	QVBoxLayout *layout = new QVBoxLayout();
	layout->addWidget(qlec);
	layout->addWidget(desc);

	//Creo groupBox que contendra al layout y lo declaro plano
	QGroupBox *gb=new QGroupBox();
	gb->setFlat(true);
	gb->setLayout(layout);
	//Creo ScrollArea que contiene groupBox
	QScrollArea *sa = new QScrollArea();
	sa->setWidget(gb);
}
//

void frmPrincipalImpl::on_action_Salir_activated()
{
	this->close();
}

void frmPrincipalImpl::on_btnBuscar_clicked()
{	
	this->paths.clear();
	this->lstResultados->clear();
	
	//JPG	
	string textoBusqueda = tolower(txtBusqueda->text().toStdString().c_str());
	Buscador buscador;	
	ListaDocumentos* listaDocs = NULL;
	
	buscador.eliminarCorchetes(textoBusqueda);
	
	if((textoBusqueda.substr(0,1).compare("\"") == 0)&&(textoBusqueda.substr(textoBusqueda.length() - 1,textoBusqueda.length()).compare("\"") == 0))
		listaDocs = buscador.RealizarBusquedaAvanzada(textoBusqueda);
	else
		listaDocs = buscador.RealizarBusqueda(textoBusqueda);
	
	if (listaDocs==NULL)
	return;
	
	buscador.ordenarPorCorchetes(listaDocs);
	
	implementacionInterprete *interpreter = new implementacionInterprete();
		
	for(unsigned int i = 0; i < listaDocs->size(); i++)
	{
		QString path = QString::fromStdString(interpreter->strObtenerDocumentoPDF((*listaDocs)[i]));
		this->paths.push_back(path);
		string aux = path.toStdString();
		QString docName = QString::fromStdString(aux.substr(aux.find_last_of('/') + 1, aux.size() - aux.find_last_of('/') - 5));
		this->lstResultados->addItem(docName);
	}
	delete listaDocs;
	//FIN JPG
	
	
	
	/*InvertedIndex ii;
	InvertedIndexData iid(palabra.size(), palabra.c_str(), 0);
	ii.search(iid);
	unsigned int docCant = iid.getDocumentsCount();
	for(unsigned int i = 0; i < docCant; i++)
	{
		std::pair<bool, unsigned int> docId = iid.getIdOfDocument(i);
		if(docId.first)
			this->lstResultados->addItem(QString::number(docId.second));
	}*/
	this->lstResultados->show();
/*	this->lstResultados->addItem(txtBusqueda->text());
	this->lbl_doc_1->show();
	this->lbl_doc_1->setText(txtBusqueda->text());
	this->lbl_desc_1->show();
	this->lbl_doc_2->show();
	this->lbl_desc_2->show();
	this->lbl_paginas->show();
	this->lbl_pag_1->show();
	this->lbl_pag_2->show();
	this->lbl_pag_3->show();
	this->lbl_pag_4->show();
	this->lbl_pag_5->show();
	this->lbl_pag_6->show();
	this->lbl_pag_7->show();
	this->lbl_pag_8->show();
	this->lbl_pag_9->show();
	this->lbl_pag_10->show();
	this->lbl_siguiente->show();*/
}


void frmPrincipalImpl::on_action_Agregar_Documentos_activated()
{
	frmAgregarDocImpl win(this);
	win.show();
	int resultado = win.exec();
}

void frmPrincipalImpl::on_lstResultados_clicked(QModelIndex index)
{
	
	//std::cout<<index.row()<<std::endl;
	QString aux = "evince \"" + paths[lstResultados->currentIndex().row()]+"\"" + " --find=\"" + txtBusqueda->text()+"\"";
	system(aux.toStdString().c_str());
}

void frmPrincipalImpl::on_action_Exportar_nodos_activated()
{
	InvertedIndex ii;
	std::ofstream out1;
	//Imprimo arbol a disco
	out1.open("./data/TreeInicial.txt", std::ios::out);
	ii.THtoOstream(out1);
	out1.close();

	//Imprimo stream file a disco
	out1.open("./data/StreamInicial.txt", std::ios::out);
	ii.SHtoOstream(out1);
	out1.close();
}

void frmPrincipalImpl::on_actionExportar_nodo_activated()
{
	frmFrontCodImpl win(this);
	win.show();
	win.exec();
}

void frmPrincipalImpl::onlinkPdfclicked(char* str){
	printf("Ejecuto link con path:%s\n", str);
	
}

void frmPrincipalImpl::on_action_Reset_ndice_activated()
{
	QString aux = QString::fromStdString(pathHash);
	remove(aux.toStdString().c_str());
	aux = "touch " + QString::fromStdString(pathHash) + ">> /dev/null";
	system(aux.toStdString().c_str());
	aux = QString::fromStdString(pathBloques);
	remove(aux.toStdString().c_str());
	aux = QString::fromStdString(pathBloquesLibres);
	remove(aux.toStdString().c_str());
	aux = QString::fromStdString(pathArbol);
	remove(aux.toStdString().c_str());
	aux = QString::fromStdString(pathArbolWordID);
	remove(aux.toStdString().c_str());
	aux = QString::fromStdString(pathID);
	remove(aux.toStdString().c_str());
	
	//borro los archivos txts ./txt
	string path = directorioTxt;
	DIR *directorio = opendir(path.c_str());
	if(directorio != NULL){
	   struct dirent *pent = NULL;
	       
	   while ((pent=readdir(directorio))){
	       if(strcmp(pent->d_name,"..")!=0 && strcmp(pent->d_name,".")!=0 && strcmp(pent->d_name,".svn")!=0 ){
            aux = QString::fromStdString(directorioTxt) + "/" + QString::fromStdString(pent->d_name);
	        remove(aux.toStdString().c_str());
           }   
	   }
    }
    
    //borro los archivos txts procesados ./txtprocesados
	path = directorioTxtProcesados;
	directorio = opendir(path.c_str());
	if(directorio != NULL){
	   struct dirent *pent = NULL;
	       
	   while ((pent=readdir(directorio))){
	       if(strcmp(pent->d_name,"..")!=0 && strcmp(pent->d_name,".")!=0 && strcmp(pent->d_name,".svn")!=0 ){
            aux = QString::fromStdString(directorioTxtProcesados) + "/" + QString::fromStdString(pent->d_name);
	        remove(aux.toStdString().c_str());
           }   
	   }
    }
	
	
	
}

void frmPrincipalImpl::on_action_About_activated()
{
	frmAboutImpl win(this);
	win.show();
	win.exec();
}

void frmPrincipalImpl::on_actionA_yuda_activated()
{
	string aux = "evince '" + pathAyuda + "'"; 
	system(aux.c_str());
}

